# Makefile for Django Hotel Management System

# Variables
COMPOSE_FILE := docker-compose.yml
PROJECT_NAME := hotel-management

# Default target
.PHONY: help
help: ## Show this help message
	@echo "Available commands:"
	@echo ""
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'

# Build services
.PHONY: build
build: ## Build all services
	docker-compose -f $(COMPOSE_FILE) build

# Start services in detached mode
.PHONY: up
up: ## Start all services in background
	docker-compose -f $(COMPOSE_FILE) up -d

# View service logs
.PHONY: logs
logs: ## View services logs
	docker-compose -f $(COMPOSE_FILE) logs -f

# Stop services
.PHONY: down
down: ## Stop all services
	docker-compose -f $(COMPOSE_FILE) down

# Restart services
.PHONY: restart
restart: ## Restart all services
	docker-compose -f $(COMPOSE_FILE) restart

# View running containers
.PHONY: ps
ps: ## List running containers
	docker-compose -f $(COMPOSE_FILE) ps

# Execute shell in web container
.PHONY: shell
shell: ## Open shell in web container
	docker-compose -f $(COMPOSE_FILE) exec web bash

# Run database migrations
.PHONY: migrate
migrate: ## Run Django database migrations
	docker-compose -f $(COMPOSE_FILE) exec web python manage.py migrate

# Create superuser
.PHONY: superuser
superuser: ## Create Django superuser
	docker-compose -f $(COMPOSE_FILE) exec web python manage.py createsuperuser

# Collect static files
.PHONY: collectstatic
collectstatic: ## Collect static files
	docker-compose -f $(COMPOSE_FILE) exec web python manage.py collectstatic --noinput

# Run tests
.PHONY: test
test: ## Run Django tests
	docker-compose -f $(COMPOSE_FILE) exec web python manage.py test

# Build services with no cache
.PHONY: rebuild
rebuild: ## Rebuild all services without cache
	docker-compose -f $(COMPOSE_FILE) build --no-cache

# Clean up unused Docker resources
.PHONY: clean
clean: ## Clean up unused Docker resources
	docker system prune -f

# Show disk usage
.PHONY: usage
usage: ## Show Docker disk usage
	docker system df -v

# MySQL 相关命令
# 进入 MySQL 容器
.PHONY: mysql-shell
mysql-shell: ## Open shell in MySQL container
	docker-compose -f $(COMPOSE_FILE) exec mysql bash

# 连接到 MySQL 数据库
.PHONY: mysql-connect
mysql-connect: ## Connect to MySQL database
	docker-compose -f $(COMPOSE_FILE) exec mysql mysql -u root -p${MYSQL_ROOT_PASSWORD:-MySQL@123} ${MYSQL_DATABASE:-hotel_management}

# 备份数据库
.PHONY: mysql-backup
mysql-backup: ## Backup MySQL database
	docker-compose -f $(COMPOSE_FILE) exec mysql mysqldump -u root -p${MYSQL_ROOT_PASSWORD:-MySQL@123} ${MYSQL_DATABASE:-hotel_management} > backups/backup_$(shell date +%Y%m%d_%H%M%S).sql

# 恢复数据库
.PHONY: mysql-restore
mysql-restore: ## Restore MySQL database (specify BACKUP_FILE)
	@if [ -z "$(BACKUP_FILE)" ]; then \
		echo "请指定备份文件: make mysql-restore BACKUP_FILE=backup_20230101_120000.sql"; \
	else \
		docker-compose -f $(COMPOSE_FILE) exec -T mysql mysql -u root -p${MYSQL_ROOT_PASSWORD:-MySQL@123} ${MYSQL_DATABASE:-hotel_management} < $(BACKUP_FILE); \
	fi

# 查看 MySQL 状态
.PHONY: mysql-status
mysql-status: ## Check MySQL service status
	docker-compose -f $(COMPOSE_FILE) exec mysql mysqladmin -u root -p${MYSQL_ROOT_PASSWORD:-MySQL@123} status

# 重启 MySQL 服务
.PHONY: mysql-restart
mysql-restart: ## Restart MySQL service
	docker-compose -f $(COMPOSE_FILE) restart mysql

# 运维相关命令
# 启动监控服务
.PHONY: monitor-up
monitor-up: ## Start monitoring services
	docker-compose -f $(COMPOSE_FILE) up -d monitor grafana

# 查看系统健康状态
.PHONY: health
health: ## Check system health status
	docker-compose -f $(COMPOSE_FILE) exec web python enhanced_ops.py --health

# 创建备份
.PHONY: backup
backup: ## Create system backup
	docker-compose -f $(COMPOSE_FILE) exec web python enhanced_ops.py --backup

# 列出备份
.PHONY: backup-list
backup-list: ## List all backups
	docker-compose -f $(COMPOSE_FILE) exec web python enhanced_ops.py --backup-list

# 恢复备份
.PHONY: backup-restore
backup-restore: ## Restore from backup (specify BACKUP_NAME)
	@if [ -z "$(BACKUP_NAME)" ]; then \
		echo "请指定备份名称: make backup-restore BACKUP_NAME=backup_full_20230101_120000"; \
	else \
		docker-compose -f $(COMPOSE_FILE) exec web python enhanced_ops.py --restore-backup $(BACKUP_NAME); \
	fi

# 清理旧备份
.PHONY: backup-cleanup
backup-cleanup: ## Clean up old backups
	docker-compose -f $(COMPOSE_FILE) exec web python enhanced_ops.py --cleanup-backups

# 启动系统监控
.PHONY: start-monitoring
start-monitoring: ## Start system monitoring
	docker-compose -f $(COMPOSE_FILE) exec web python enhanced_ops.py --start-monitoring

# 停止系统监控
.PHONY: stop-monitoring
stop-monitoring: ## Stop system monitoring
	docker-compose -f $(COMPOSE_FILE) exec web python enhanced_ops.py --stop-monitoring

# 分析日志
.PHONY: analyze-logs
analyze-logs: ## Analyze system logs
	docker-compose -f $(COMPOSE_FILE) exec web python enhanced_ops.py --analyze-logs

# 查看监控数据
.PHONY: monitor-data
monitor-data: ## View monitoring data
	docker-compose -f $(COMPOSE_FILE) exec web python enhanced_ops.py --monitor-data